package it.struts.helloworld.services;

import it.struts.helloworld.model.Utenti;
import it.struts.helloworld.utils.HibernateUtils;

import org.hibernate.HibernateException;
//import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

//public class NewUserService {
//	
//	Session sess;
//	
//	public Utenti retrieveDetails(String nome, String cognome, String data, String cf, String indirizzo, String mail, String username, String password, String tipo){
//	
//		try{
//			sess= HibernateUtils.getSessionFactory();
//			Query query = sess.createQuery("INSERT INTO utenti (nome, cognome, data, cf, indirizzo, mail, username, password, tipo) VALUES (:nome, :cognome, :data, :cf, :indirizzo, :mail, :username, :password, :tipo)");
//			query.setParameter("nome", nome);
//			query.setParameter("cognome", cognome);
//			query.setParameter("data", data);
//			query.setParameter("cf", cf);
//			query.setParameter("indirizzo", indirizzo);
//			query.setParameter("mail", mail);
//			query.setParameter("username",username);
//			query.setParameter("password", password);
//			query.setParameter("tipo", tipo);
//			
//			query.executeUpdate();
//	
//		}
//		catch(Exception exep){
//			exep.printStackTrace();
//		}
//		
//		return null;
//	}
//
//
//}

public class NewUserService {

	Session sess;
	

    public void addNewUtente(String nome, String cognome, String data, String cf, String indirizzo, String mail, String username, String password)

            throws Exception {

        try {

        	sess= HibernateUtils.getSessionFactory();

            Transaction tx = sess.beginTransaction();

            Utenti u = new Utenti();

            String name = nome;
            String surname = cognome;
            String date= data;
            String codeF = cf;
            String address = indirizzo;
            String eMail = mail;
            String us = username;
            String psw = password;
          

            u.setNome(name);
            u.setCognome(surname);
            u.setData(date);
            u.setCF(codeF);
            u.setIndirizzo(address);
            u.setMail(eMail);
            u.setUsername(us);
            u.setPassword(psw);
            u.setTipo("Utente");


            sess.save(u);
            tx.commit();


        } catch (HibernateException e) {

        
        }

 

    }

 

}

